package com.llc.mall.user.infrastructure.gatewayimpl;

import com.llc.mall.common.core.exception.Mall4cloudException;
import com.llc.mall.common.core.response.ResponseEnum;
import com.llc.mall.common.core.response.ServerResponseEntity;
import com.llc.mall.user.domain.gateway.SegmentGateway;
import com.llc.mall.user.infrastructure.persist.po.UserPo;
import com.mall4j.cloud.api.leaf.feign.SegmentFeignClient;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

@Slf4j
@Service
public class SegmentGatewayImpl implements SegmentGateway {

    private final SegmentFeignClient segmentFeignClient;

    public SegmentGatewayImpl(SegmentFeignClient segmentFeignClient){
        this.segmentFeignClient = segmentFeignClient;
    }

    @Override
    public Long generateId() {
        ServerResponseEntity<Long> segmentIdResponse = segmentFeignClient.getSegmentId(UserPo.DISTRIBUTED_ID_KEY);
        if (!segmentIdResponse.isSuccess()) {
            throw new Mall4cloudException(ResponseEnum.EXCEPTION);
        }
        return segmentIdResponse.getData();
    }
}
